﻿using Nop.Admin.Models.XJ;
using Nop.Core;
using Nop.Core.Domain.XJ;
using Nop.Services.Common;
using Nop.Services.Customers;
using Nop.Services.Directory;
using Nop.Services.Logging;
using Nop.Services.Media;
using Nop.Services.Security;
using Nop.Services.XJ;
using Nop.Web.Framework.Controllers;
using Nop.Web.Framework.Kendoui;
using System;
using System.Linq;
using System.Web.Mvc;

namespace Nop.Admin.Controllers
{
	public class MaintenanceCommentController : BaseAdminController
	{
		#region Fields
		private readonly IPermissionService _permissionService;
		private readonly IPictureService _pictureService;
		private readonly IWorkContext _workContext;
		private readonly IOrganizationService _organizationServce;
		private readonly ICustomerService _customerService;
		private readonly IGenericAttributeService _genericAttributeService;
		private readonly IXJEquipmentCategoryService _xJEquipmentCategoryService;
		private readonly IXJEquipmentService _xJEquipmentService;
		private readonly IXJEquipmentStockService _xJEquipmentStockService;
		private readonly IXJInspectionService _xJInspectionService;
		private readonly IXJInspectionPlanService _xJInspectionPlanService;
		private readonly IXJMaintenanceService _xJMaintenanceService;
		private readonly IXJMaintenanceCommentService _xJMaintenanceCommentService;
		private readonly ICustomerActivityService _customerActivityService;
		private readonly ILogger _logger;
		#endregion

		#region Constructors
		public MaintenanceCommentController(IPermissionService permissionService,
			IPictureService pictureService,
			IWorkContext workContext,
			IOrganizationService organizationServce,
			ICustomerService customerService,
			IGenericAttributeService genericAttributeService,
			IXJEquipmentCategoryService xJEquipmentCategoryService,
			IXJEquipmentService xJEquipmentService,
			IXJEquipmentStockService xJEquipmentStockService,
			IXJInspectionService xJInspectionService,
			IXJInspectionPlanService xJInspectionPlanService,
			IXJMaintenanceService xJMaintenanceService,
			IXJMaintenanceCommentService xJMaintenanceCommentService,
			ICustomerActivityService customerActivityService,
			ILogger logger)
		{
			this._permissionService = permissionService;
			this._pictureService = pictureService;
			this._workContext = workContext;
			this._organizationServce = organizationServce;
			this._customerService = customerService;
			this._genericAttributeService = genericAttributeService;
			this._xJEquipmentCategoryService = xJEquipmentCategoryService;
			this._xJEquipmentService = xJEquipmentService;
			this._xJEquipmentStockService = xJEquipmentStockService;
			this._xJInspectionService = xJInspectionService;
			this._xJInspectionPlanService = xJInspectionPlanService;
			this._xJMaintenanceService = xJMaintenanceService;
			this._xJMaintenanceCommentService = xJMaintenanceCommentService;
			this._customerActivityService = customerActivityService;
			this._logger = logger;
		}
		#endregion

		#region Utilities

		#endregion

		#region List
		public ActionResult Index()
		{
			return RedirectToAction("List");
		}

		public virtual ActionResult List()
		{
			if (!_permissionService.Authorize(StandardPermissionProvider.ManageMaintenances))
				return AccessDeniedView();

			var model = new MaintenanceCommentListModel();

			return View(model);
		}

		[HttpPost]
		public virtual ActionResult List(DataSourceRequest command, MaintenanceCommentListModel model)
		{
			if (!_permissionService.Authorize(StandardPermissionProvider.ManageMaintenances))
				return AccessDeniedKendoGridJson();

			var categories = _xJMaintenanceCommentService.GetAllMaintenanceComments(
				maintenanceId: model.SearchMaintenanceId,
				pageIndex: command.Page - 1,
				pageSize: command.PageSize);
			var gridModel = new DataSourceResult
			{
				Data = categories.Select(x =>
				{
					var categoryModel = new MaintenanceCommentModel
					{
						Id = x.Id,
						MaintenanceId = x.MaintenanceId,
						PId = x.PId,
						Comment = x.Comment,
						Star = x.Star,
						CustomerId = x.CustomerId,
						CreatedOn = x.CreatedOn,
					};
					return categoryModel;
				}),
				Total = categories.TotalCount
			};
			return Json(gridModel);
		}
		#endregion

		#region Create / Edit / Delete

		public virtual ActionResult Create()
		{
			if (!_permissionService.Authorize(StandardPermissionProvider.ManageMaintenances))
				return AccessDeniedView();

			var model = new MaintenanceCommentModel();

			return View(model);
		}

		[HttpPost, ParameterBasedOnFormName("save-continue", "continueEditing")]
		public virtual ActionResult Create(MaintenanceCommentModel model, bool continueEditing)
		{
			if (!_permissionService.Authorize(StandardPermissionProvider.ManageMaintenances))
				return AccessDeniedView();

			if (ModelState.IsValid)
			{
				var equipment = new XJMaintenanceComment
				{
					MaintenanceId = model.MaintenanceId,
					PId = model.PId,
					Comment = model.Comment,
					Star = model.Star,
					CustomerId = _workContext.CurrentCustomer.Id,
					CreatedOn = DateTime.Now,
				};
				_xJMaintenanceCommentService.InsertXJMaintenanceComment(equipment);

				//activity log
				_customerActivityService.InsertActivity("AddNewEquipmentCategory", "新增维修评价：{0}", equipment.Id);

				SuccessNotification("保存成功");

				if (continueEditing)
				{
					//selected tab
					SaveSelectedTabName();

					return RedirectToAction("Edit", new { id = equipment.Id });
				}
				return RedirectToAction("List");
			}

			return View(model);
		}

		public virtual ActionResult Edit(int id)
		{
			if (!_permissionService.Authorize(StandardPermissionProvider.ManageMaintenances))
				return AccessDeniedView();

			var equipment = _xJMaintenanceCommentService.GetXJMaintenanceCommentById(id);
			if (equipment == null || equipment.Deleted)
				//No equipment found with the specified id
				return RedirectToAction("List");

			var model = new MaintenanceCommentModel
			{
				Id = equipment.Id,
				MaintenanceId = equipment.MaintenanceId,
				PId = equipment.PId,
				Comment = equipment.Comment,
				Star = equipment.Star,
				CustomerId = equipment.CustomerId,
				CreatedOn = equipment.CreatedOn,
			};

			return View(model);
		}

		[HttpPost, ParameterBasedOnFormName("save-continue", "continueEditing")]
		public virtual ActionResult Edit(MaintenanceCommentModel model, bool continueEditing)
		{
			if (!_permissionService.Authorize(StandardPermissionProvider.ManageMaintenances))
				return AccessDeniedView();

			var equipment = _xJMaintenanceCommentService.GetXJMaintenanceCommentById(model.Id);
			if (equipment == null || equipment.Deleted)
				//No equipment found with the specified id
				return RedirectToAction("List");

			if (ModelState.IsValid)
			{
				equipment.MaintenanceId = model.MaintenanceId;
				equipment.PId = model.PId;
				equipment.Comment = model.Comment;
				equipment.Star = model.Star;
				_xJMaintenanceCommentService.UpdateXJMaintenanceComment(equipment);

				//activity log
				_customerActivityService.InsertActivity("EditEquipmentCategory", "编辑维修评价：{0}", equipment.Id);

				SuccessNotification("保存成功");
				if (continueEditing)
				{
					//selected tab
					SaveSelectedTabName();

					return RedirectToAction("Edit", new { id = equipment.Id });
				}
				return RedirectToAction("List");
			}

			return View(model);
		}

		[HttpPost]
		public virtual ActionResult Delete(int id)
		{
			if (!_permissionService.Authorize(StandardPermissionProvider.ManageMaintenances))
				return AccessDeniedView();

			var equipment = _xJMaintenanceCommentService.GetXJMaintenanceCommentById(id);
			if (equipment == null)
				//No equipment found with the specified id
				return RedirectToAction("List");

			_xJMaintenanceCommentService.DeleteXJMaintenanceComment(equipment);

			//activity log
			_customerActivityService.InsertActivity("DeleteEquipmentCategory", "删除维修评价：{0}", equipment.Id);

			SuccessNotification("删除成功");
			return RedirectToAction("List");
		}


		#endregion
	}
}